﻿package org.openChart.sprite 
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import org.openChart.display.DisplayTextLabel;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class TextLabelSprite extends BaseSprite
	{
		//public var text:DisplayTextLabel;
		
		[Embed(systemFont = 'Arial', fontName = 'spArial', mimeType = 'application/x-font')]
		private static var ArialFont:Class;
		private var textField:TextField = new TextField();
		
		public function TextLabelSprite( text:DisplayTextLabel, commandHander:CommandHandler ) 
		{
			super(text, commandHander);
		}
		
		public override function paint():void
		{
			var text:DisplayTextLabel = displayObject as DisplayTextLabel;
			
			textField.text = text.text;
			
			var textFormat:TextFormat = new TextFormat();
			if (text.align.indexOf("left") > 0)
			{
				textFormat.align = "left";
			}
			else if (text.align.indexOf("right") > 0)
			{
				textFormat.align = "right";
			}
			else if (text.align.indexOf("center") > 0)
			{
				textFormat.align = "center";
			}
			textFormat.size = text.textSize;
			textFormat.font = "spArial";
			textFormat.color = text.color;
			
			textField.setTextFormat(textFormat);
			textField.embedFonts = true;
			textField.width = textField.textWidth + 5;
			textField.height = textField.textHeight;
			textField.visible = true;
			
			if (text.rotation > 1)
			{
				textField.embedFonts = true;
				textField.rotation = text.rotation;
				textField.antiAliasType = AntiAliasType.ADVANCED;
			}
			switch(text.align)
			{
				case "center":
					textField.autoSize = TextFieldAutoSize.CENTER;
					this.x = text.position.x - textField.width / 2;
					this.y = text.position.y - textField.height / 2;
					break;
				case "center-left":
					textField.autoSize = TextFieldAutoSize.LEFT;
					this.x = text.position.x;
					this.y = text.position.y - textField.height / 2;
					break;
				case "center-right":
					textField.autoSize = TextFieldAutoSize.RIGHT;
					this.x = text.position.x - textField.width;
					this.y = text.position.y - textField.height / 2;
					break;
				case "center-top":
					textField.autoSize = TextFieldAutoSize.CENTER;
					this.x = text.position.x - textField.width / 2;
					this.y = text.position.y;
					break;
				case "center-bottom":
					textField.autoSize = TextFieldAutoSize.CENTER;
					this.x = text.position.x - textField.width / 2;
					this.y = text.position.y - textField.height;
					break;
				case "top-left":
					textField.autoSize = TextFieldAutoSize.LEFT;
					this.x = text.position.x;
					this.y = text.position.y;
					break;
				case "top-right":
					textField.autoSize = TextFieldAutoSize.RIGHT;
					this.x = text.position.x - textField.width;
					this.y = text.position.y;
					break;
				case "bottom-left":
					textField.autoSize = TextFieldAutoSize.RIGHT;
					this.x = text.position.x;
					this.y = text.position.y - textField.height;
					break;
				case "bottom-right":
					textField.autoSize = TextFieldAutoSize.LEFT;
					this.x = text.position.x - textField.width;
					this.y = text.position.y - textField.height;
					break;
				default:
					throw new Error("can't support " + text.align + " type");
			}
			this.addChild(textField);
		}
	}
	
}